const Db = require('../services/mysql')
const { Fb, Moment, Op, Dev } = require('../class/utils')
const _ = require('lodash')

class News {
  constructor() {}

  /**
   * 新增新闻
   * @param {*} where
   */
  create(form) {
    return Db.News.create({
      title: form.title,
      content: form.content,
      savetime: Moment.current(),
      img: form.img
    })
  }

  /**
   * 查找新闻列表
   * @param {*} where
   */
  select(form, nowPage, pageSize) {
    let title = form.title ||'';
    return Db.News.findAndCountAll({
      where: {
        title: {
          [Op.like]: '%' + title + '%'
        }
      },
      order: [
        ['savetime', 'DESC']
      ],
      offset: pageSize * (nowPage - 1),
      limit: pageSize
    })
  }

  /**
   * 新闻详情
   * @param {*} where
   */
  details(id) {
    return Db.News.findById(id)
  }

  /**
   * 修改新闻信息
   * @param {*} where
   */
  update(form) {
    return Db.News.update(
      {
        title: form.title,
        content: form.content,
        savetime: Moment.current(),
        img: form.img
      },
      {
        where: {
          id: form.id
        }
      }
    )
  }

  /**
   * 删除新闻
   * @param {*} where
   */
  delete(id) {
    return Db.News.destroy({
      where: {
        id: id
      }
    })
  }
}

module.exports = new News()
